package lab1_ArrayList;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ListIterator;
import java.util.NoSuchElementException;

public class Main {

	@SuppressWarnings("resource")
	public static void main(String[] args) {
		
		BufferedReader in = null;
		PrintWriter out = null;
		
		try {
			if (args.length!=0){
				if (args.length == 2){
					in = new BufferedReader(new FileReader(args[0]+".txt"));
					out = new PrintWriter(new FileWriter(args[1]+".txt"));
				} else {
					throw new FileNotFoundException("Entered not 2 files");
				}
			}else{
				in = new BufferedReader(new FileReader("in.txt"));
				out = new PrintWriter(new FileWriter("out.txt"));
			}
			
			MyArrayList<Integer> myList=new MyArrayList<Integer>();
			ListIterator<Integer> iterator=myList.listIterator();
			int x;
			
			String operation = in.readLine();
	        String[] split;
	        
	        if (operation==null) {
	        	out.println("Operation not found");
	        	return;
	        }
	        
	        while (operation != null){
	        	split = operation.split(" ");
	        	
		        if (split[0].equals("add")) {
		        	if (split.length == 2){
			        	x=Integer.parseInt(split[1]);
		        		myList.add(x);
		        		out.println("ArrayList after adding: "+myList.toString());
		        	} else {
		        		throw new NullPointerException("Object should not be null");
		        	}
		        } else if (split[0].equals("clear")){
		        	myList.clear();
	        		if (myList.isEmpty()) {
	        			out.println("ArrayList is clear");
	        		} else {
	        			out.println("Could not clear ArrayList");
	        		}
	        		
		        } else if(split[0].equals("isEmpty")){
		        	if (myList.isEmpty()){
		        		out.println("ArrayList is empty");
		        	} else {
		        		out.println("ArrayList is not empty");
		        	}
		        	
				} else if (split[0].equals("removeObject")){
					if (split.length == 2){
						x=Integer.parseInt(split[1]);
		        		myList.removeObject(x);
		        		out.println("ArrayList after removing "+x+" : "+myList.toString());
					} else {
						throw new NullPointerException("Object should not be null");
					}
	        		
				} else if(split[0].equals("removeByIndex")){ 
					if (split.length == 2){ 
						x=Integer.parseInt(split[1]);
						myList.removeByIndex(x);
						out.println("ArrayList after removing by index "+x+" : "+myList.toString());
					} else {
						throw new NullPointerException("Index should not be null");
					}
					
				} else if (split[0].equals("size")){
					out.println("Size= "+myList.size());
					
				} else if (split[0].equals("get")){
					if (split.length == 2){
						x=Integer.parseInt(split[1]);
						out.println("Element by index "+x+" = "+myList.get(x));
					} else {
						throw new NullPointerException("Index should not be null");
					}
	        				
				} else if (split[0].equals("toString")){
					out.println("ArrayList: "+myList.toString());
					
				} else if (split[0].equals("next")){
					if (iterator.hasNext()){
	        			out.println("Next element = "+iterator.next());
				    } else {
				    	out.println("End of ArrayList");
				    }
					
				} else if (split[0].equals("previous")){
					if (iterator.hasPrevious()){
						out.println("Previous element = "+iterator.previous());
					} else {
						out.println("Start of ArrayList");
					}
					
				} else if (split[0].equals("hasNext")){
					if (iterator.hasNext()){
						out.println("Next element exists");
					} else {
						out.println("End of ArrayList");
					}
					
				} else if (split[0].equals("hasPrevious")){
					if (iterator.hasPrevious()){
						out.println("Previous element exists");
					} else {
						out.println("Start of ArrayList");
					}
					
				} else {
					out.println("Incorrect operation");
				}
		        
		        operation=in.readLine();
	        }
		} catch(FileNotFoundException e){
			System.out.println(e.toString());
	        
		} catch(IOException e){
			System.out.println(e.toString());
			
		} catch(NumberFormatException e) {
			out.println("Operation need to input number");
	        
		} catch(IndexOutOfBoundsException e) {
			out.println(e.toString());
			
		} catch(NoSuchElementException e) {
			out.println(e.toString());
			
		} catch(NullPointerException e) {
			out.println(e.toString());
			
		} finally {
			try{
				if (in != null) in.close();
				if (out != null) out.close();
			} catch (IOException e) {
				System.out.println(e.toString());
			}
		}
	}
}
